﻿using System;
using IQIGame.Onigao.Framework.Editors;
using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class ParticleControlDataInspector : OdinValueDrawer<EffectControlBehaviour.ParticleControlData>
    {
        protected override void DrawPropertyLayout(GUIContent label)
        {
            var entry = this.ValueEntry;
            var variable = entry.SmartValue;
            bool dirty = false;
            EditorGUILayout.BeginVertical();
            {
                var nControlTypeInt = (int)variable.controlType;
                if (BattleEditorTools.EnumPopup(typeof(EffectControlBehaviour.EffectControlType), "特效控制类型", ref nControlTypeInt))
                {
                    variable.controlType = (EffectControlBehaviour.EffectControlType)nControlTypeInt;
                    dirty = true;
                }
                var nStageTypeInt = (int)variable.stageType;
                if (BattleEditorTools.EnumPopup(typeof(EffectControlBehaviour.EEffectStageType), "特效阶段类型", ref nStageTypeInt))
                {
                    variable.stageType = (EffectControlBehaviour.EEffectStageType)nStageTypeInt;
                    dirty = true;
                }
                if (BattleEditorTools.Toggle("仅水平旋转", ref variable.onlyRotateY))
                {
                    dirty = true;
                }
                switch (variable.controlType)
                {
                    case EffectControlBehaviour.EffectControlType.None:
                        dirty |= BattleEditorTools.ObjectField("子弹特效", ref variable.particle);
                        break;
                    case EffectControlBehaviour.EffectControlType.Bullet:
                        dirty |= BattleEditorTools.ObjectField("子弹特效", ref variable.particle);
                        if (variable.paramInt == null || variable.paramInt.Length < 2)
                        {
                            Array.Resize(ref variable.paramInt, 2);
                        }
                        dirty |= BattleEditorTools.Toggle("保持子弹默认旋转", ref variable.paramInt[0]);
                        dirty |= BattleEditorTools.Toggle("跟随目标缩放", ref variable.paramInt[1]);
                        break;
                    case EffectControlBehaviour.EffectControlType.Laser:
                        if (variable.particles == null || variable.particles.Length < 2)
                        {
                            variable.particles = new ParticleSystem[2];
                        }
                        dirty |= BattleEditorTools.ObjectField("激光控制", ref variable.particle);
                        dirty |= BattleEditorTools.ObjectField("激光起点", ref variable.particles[0]);
                        dirty |= BattleEditorTools.ObjectField("激光终点", ref variable.particles[1]);
                        dirty |= BattleEditorTools.ObjectField("激光", ref variable.LineRenderer);
                        break;
                    case EffectControlBehaviour.EffectControlType.OtherDummy:
                        if (variable.paramInt == null || variable.paramInt.Length < 1)
                        {
                            Array.Resize(ref variable.paramInt, 1);
                        }
                        dirty |= BattleEditorTools.ObjectField("特效", ref variable.particle);
                        dirty |= BattleEditorTools.IntField("骨骼点", ref variable.paramInt[0]);
                        break;
                }
            }
            EditorGUILayout.EndVertical();
            if (dirty)
            {
                entry.SmartValue = variable;
                entry.ApplyChanges();
                EditorUtility.SetDirty(entry.Property.Tree.UnitySerializedObject.targetObject);
            }
        }
    }
}